/**
 * Created by 小敏哥 on 2017/11/27.
 */
import {CAR_LIST} from "./actionsTypes";
import commonService from "../../../../../js/services/commonService";
import {Toast} from "antd-mobile";

export const carList = data => ({type: CAR_LIST, data: data});

export const getCarList = (data, callBack) => {
    let carInfoList = [];
    let responseCount = 0;
    return (dispatch, getState) => {
        Toast.hide();
        Toast.loading("", 30, () => Toast.info("网络错误", 2));
        commonService.post(data, 'emsc-partner/b/carViola/selectCars.ajax').then(resultData => {
            if (resultData.code == '1000') {
                if (resultData.body.length > 0) {
                    for (let i=0;i<resultData.body.length;i++) {
                        commonService.post({carId: resultData.body[i].carId}, 'emsc-partner/b/carViola/selectCarInfo.ajax').then(infoResult => {
                            responseCount++;
                            if (infoResult.code == '1000') {
                                carInfoList[i]={
                                    carId: infoResult.body.carId,
                                    carCode: infoResult.body.carCode,
                                    carNumber: infoResult.body.carNumber,
                                    degreeCount: infoResult.body.degreeCount,
                                    fineTotalAmount: infoResult.body.fineTotalAmount,
                                    voliationCount: infoResult.body.voliationCount,
                                };
                                if (responseCount == resultData.body.length) {
                                    Toast.hide();
                                    dispatch(carList(carInfoList));
                                }
                            }
                            else {
                                Toast.info(resultData.msg ? resultData.msg : '网络错误', 2);
                            }
                        })
                    }
                }
                else {
                    Toast.hide();
                    dispatch(carList([]));
                }
            }
            else {
                Toast.info(resultData.msg ? resultData.msg : '网络错误', 2);
            }
        }).catch((e) => {
            Toast.info("网络错误", 2);
            throw e;
        })
    }

    /*
        return commonAction.simplePost(data,'emsc-partner/b/carViola/selectCars.ajax',undefined,(result)=>{
            for(let item of result){
                commonAction.simplePost({carId:item.carId},'emsc-partner/b/carViola/selectCarInfo.ajax',undefined,(infoResult)=>{
                    carInfoList.push({
                        carId:infoResult.carId,
                        carCode:infoResult.carCode,
                        carNumber:infoResult.carNumber,
                        degreeCount:infoResult.degreeCount,
                        fineTotalAmount:infoResult.fineTotalAmount,
                    });
                    if(carInfoList.length==result.length){

                    }
                })
            }
        });*/
};